﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;
using Org.Mentalis.Network;

namespace ToRTF
{
    public class WordDocHandler
    {

        Dictionary<String, Object> typeTable = new Dictionary<String, Object>();


        public void Init()
        {


            typeTable["html"] = WdSaveFormat.wdFormatHTML;
            typeTable["txt"] = WdSaveFormat.wdFormatUnicodeText;
            typeTable["rtf"] = WdSaveFormat.wdFormatRTF;
            typeTable["xps"] = WdSaveFormat.wdFormatXPS;
            typeTable["pdf"] = WdSaveFormat.wdFormatPDF;


        }

        public void CleanUp()
        {
            OfficeObjectPool<Application>.Instance.shutDownPool();
        }

        static Object missing = System.Reflection.Missing.Value;





        public String SaveAs(String fileName, ref string fileName2, String format)
        {

            object readOnly = true;
            // we can keep it false if you want to open the file but not make it invisible
            object isVisible = true;
            object openAndRepair = false;
            object noEncodingDialog = false;
            Object istrue = true;
            Object oFalse = false;
            Object oFalse2 = false;

            Object fileName1 = fileName;
            _Application oWord = OfficeObjectPool<Application>.Instance.GetObject();
            _Document oWordDoc = null;
            try
            {
                oWordDoc = oWord.Documents.Open(
                   ref fileName1,
                   ref oFalse,
                   ref readOnly, //yes
                   ref missing,
                   ref missing,
                   ref missing,
                   ref missing,
                   ref missing,
                   ref missing,
                   ref missing,
                   ref missing,
                   ref isVisible, //false
                   ref openAndRepair, //false
                   ref missing,
                   ref noEncodingDialog, //true
                   ref missing);

                object format2 = typeTable[format];

                object fileName3 = fileName2;
                oWordDoc.SaveEncoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;

                /* http://msdn.microsoft.com/de-de/library/microsoft.office.interop.word._document.saveas(office.11).aspx
                 * [In, Optional] ref object FileName, 
    [In, Optional] ref object FileFormat, 
    [In, Optional] ref object LockComments, 
    [In, Optional] ref object Password, 
    [In, Optional] ref object AddToRecentFiles, 
    [In, Optional] ref object WritePassword, 
    [In, Optional] ref object ReadOnlyRecommended, 
    [In, Optional] ref object EmbedTrueTypeFonts, 
    [In, Optional] ref object SaveNativePictureFormat, 
    [In, Optional] ref object SaveFormsData, 
    [In, Optional] ref object SaveAsAOCELetter, 
    [In, Optional] ref object Encoding, 
    [In, Optional] ref object InsertLineBreaks, 
    [In, Optional] ref object AllowSubstitutions, 
    [In, Optional] ref object LineEnding, 
    [In, Optional] ref object AddBiDiMarks*/

                object encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;
                oWordDoc.SaveAs(
                         ref fileName3,
                         ref format2,
                         ref  readOnly,
                         ref missing,
                         ref missing,
                         ref missing,
                         ref missing,
                         ref missing,
                         ref missing,
                         ref missing,
                         ref missing,
                         ref encoding,
                         ref missing,
                         ref missing,
                         ref missing,
                         ref missing
                    );
                //oWordDoc.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (oWordDoc != null) oWordDoc.Close(ref missing, ref missing, ref missing);
                OfficeObjectPool<Application>.Instance.Release((Application)oWord);
            }
            return fileName2;
        }
    }
}
